package org.jeecg.modules.assets.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.assets.dto.*;
import org.jeecg.modules.assets.entity.AssetsPreliminaryResults;
import org.jeecg.modules.assets.entity.AssetsRandomStock;
import org.jeecg.modules.assets.service.IAssetsRandomStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Arrays;

/**
 * @Description: 抽盘管理
 * @Author: jeecg-boot
 * @Date: 2023-12-13
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "移动端抽盘管理")
@RestController
@RequestMapping("app/assets/assetsRandomStock")
public class AppAssetsRandomStockController extends JeecgController<AssetsRandomStock, IAssetsRandomStockService> {
    @Autowired
    private IAssetsRandomStockService assetsRandomStockService;

    /**
     * 分页列表查询
     *
     * @param assetsRandomStock
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "抽盘管理-分页列表查询")
    @ApiOperation(value = "抽盘管理-分页列表查询", notes = "抽盘管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AssetsRandomStock assetsRandomStock,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AssetsRandomStock> queryWrapper = QueryGenerator.initQueryWrapper(assetsRandomStock, req.getParameterMap());
        Page<AssetsRandomStock> page = new Page<AssetsRandomStock>(pageNo, pageSize);
        //按照创建时间倒序排列
        queryWrapper.orderByDesc("create_time");
        IPage<AssetsRandomStock> pageList = assetsRandomStockService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 添加
     *
     * @param
     * @return
     */
    @AutoLog(value = "抽盘管理-创建")
    @ApiOperation(value = "抽盘管理-创建", notes = "抽盘管理-创建")
    @PostMapping(value = "/startAssets")
    public Result<?> startAssets(@RequestBody StartAssetsDTO startAssets) throws ParseException {
        return assetsRandomStockService.startAssets(startAssets);

    }

    /**
     * 添加
     *
     * @param assetsRandomStock
     * @return
     */
    @AutoLog(value = "抽盘管理-添加")
    @ApiOperation(value = "抽盘管理-添加", notes = "抽盘管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AssetsRandomStock assetsRandomStock) {
        assetsRandomStockService.save(assetsRandomStock);
        return Result.OK("添加成功！");
    }


    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    @AutoLog(value = "初盘结果管理-获取用户初盘信息")
    @ApiOperation(value = "初盘结果管理-获取用户初盘信息", notes = "初盘结果管理-获取用户初盘信息")
    @GetMapping(value = "/getUsetAssetsInfo")
    public Result<AssetsListRandomDTO> getUsetAssetsInfo(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "billNo") String billNo,
            @RequestParam(name = "queryType") Integer queryType) {
        LambdaQueryWrapper<AssetsRandomStock> lambda = new QueryWrapper<AssetsRandomStock>().lambda();
        lambda.eq(AssetsRandomStock::getBillNo, billNo);
        AssetsRandomStock assetsRandomStock = assetsRandomStockService.getOne(lambda);
        Page page = new Page<>(pageNo, pageSize);
        AssetsListRandomDTO usetAssetsInfo = assetsRandomStockService.getUsetAssetsInfo(assetsRandomStock, page, queryType);
        return Result.OK(usetAssetsInfo);
    }


    @AutoLog(value = "抽盘管理-操作抽盘明细")
    @ApiOperation(value = "初盘结果管理-操作抽盘明细", notes = "初盘结果管理-操作抽盘明细")
    @PostMapping(value = "/optionAssetsItem")
    public Result optionAssetsItem(@RequestBody OptionAssetsItemDTO optionAssetsItemDTO) {
        try {
            assetsRandomStockService.optionAssetsItem(optionAssetsItemDTO);
        } catch (Exception e) {
            log.error("操作抽盘明细失败", e);
            return Result.error("操作抽盘明细失败:" + e.getMessage());
        }

        return Result.OK();
    }



    /**
     * 抽盘刷新
     */
    @AutoLog(value = "抽盘管理-刷新")
    @ApiOperation(value = "抽盘管理-刷新", notes = "抽盘管理-刷新")
    @GetMapping(value = "/refresh")
    public Result<?> refresh(@RequestParam(name = "billNo") String billNo) {
        assetsRandomStockService.refresh(billNo);
        return Result.OK("刷新成功!");
    }

    /**
     * 编辑
     *
     * @param assetsRandomStock
     * @return
     */
    @AutoLog(value = "抽盘管理-编辑")
    @ApiOperation(value = "抽盘管理-编辑", notes = "抽盘管理-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<?> edit(@RequestBody AssetsRandomStock assetsRandomStock) {
        assetsRandomStockService.updateById(assetsRandomStock);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "抽盘管理-通过id删除")
    @ApiOperation(value = "抽盘管理-通过id删除", notes = "抽盘管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        assetsRandomStockService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "抽盘管理-批量删除")
    @ApiOperation(value = "抽盘管理-批量删除", notes = "抽盘管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.assetsRandomStockService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "抽盘管理-通过id查询")
    @ApiOperation(value = "抽盘管理-通过id查询", notes = "抽盘管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AssetsRandomStock assetsRandomStock = assetsRandomStockService.getById(id);
        return Result.OK(assetsRandomStock);
    }

    /**
     * 抽盘结束
     */
    @AutoLog(value = "抽盘管理-结束抽盘")
    @ApiOperation(value = "抽盘管理-结束抽盘", notes = "抽盘管理-结束抽盘")
    @PostMapping(value = "/endAssets")
    public Result<?> endAssets(@RequestBody EndAssetsDTO endAssetsDTO) {
        assetsRandomStockService.endAssets(endAssetsDTO);
        return Result.OK("结束成功！");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param assetsRandomStock
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AssetsRandomStock assetsRandomStock) {
        return super.exportXls(request, assetsRandomStock, AssetsRandomStock.class, "抽盘管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AssetsRandomStock.class);
    }

}
